package com.csyf.manager.service.impl;

import com.csyf.manager.channel.base.ServiceEnum;
import com.csyf.manager.dao.SysPaymentChannelDao;
import com.csyf.manager.entity.SysChannelAttr;
import com.csyf.manager.entity.SysPaymentChannel;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.service.SysPaymentChannelBiz;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: Casper
 * @Date: 2018-7-9 11:37:43
 */
@Slf4j
@Service
public class SysPaymentChannelBizImpl implements SysPaymentChannelBiz {

    @Autowired
    private SysPaymentChannelDao sysPaymentChannelDao;

    @Autowired
    private RedisService redisService;


    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;

    /**
     * 根据商户号查询代付通道
     *
     * @param mch_id
     * @param src_mch_id
     * @return
     */
    @Override
    public SysPaymentChannel queryByMchid(String mch_id, String src_mch_id) {
        SysPaymentChannel record = new SysPaymentChannel();
        record.setMchId(mch_id);
        record.setMerchantNo(src_mch_id);
        return sysPaymentChannelDao.selectOne(record);
    }

    /**
     * 查询上游代付通道号
     *
     * @param mch_id
     * @param service
     * @return
     * @throws ApiValidateException
     */
    @Override
    public SysPaymentChannel handleMerchant(String mch_id, String service) throws ApiValidateException {

        SysPaymentChannel record = new SysPaymentChannel();
        List<SysPaymentChannel> select = new ArrayList<>();

        SysChannelAttr tradeByService = sysChannelAttrBiz.findTradeByService(service);
        if (null == tradeByService) {
            throw new ApiValidateException("尚未配置上游代付通道号，请联系技术");
        } else {
            record.setMchId(mch_id);
            record.setChannelNo(tradeByService.getChannelName());
        }

        record.setStatus(1);
        select = sysPaymentChannelDao.select(record);
        if (select.isEmpty()) {
            log.error("商户号[{}] 业务[{}],未开通", mch_id, service);
            throw new ApiValidateException("代付通道信息未配置");
        }

        int i = new Random().nextInt(select.size());
        SysPaymentChannel sysPaymentChannel = select.get(i);

        redisService.setPaymentChannelInfo(sysPaymentChannel.getMchId(), sysPaymentChannel.getMerchantNo(), new Gson().toJson(sysPaymentChannel));
        return sysPaymentChannel;
    }


    /**
     * 查询支付通道列表
     *
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @Override
    public JqGridVo querydata(SysPaymentChannel vo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("mch_id desc");

        Example example = new Example(SysPaymentChannel.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "agentId", vo.getAgentId());
        PageUtils.andEqualTo(criteria, "mchId", vo.getMchId());
        PageUtils.andEqualTo(criteria, "merchantNo", vo.getMerchantNo());
        PageUtils.andLikeTo(criteria, "channelNo", vo.getChannelNo());
        List<SysPaymentChannel> select = sysPaymentChannelDao.selectByExample(example);

        JqGridVo jqGridVo = PageUtils.pasrJqGrid(select);
        return jqGridVo;
    }


    /**
     * 更新代付通道状态
     *
     * @param vo
     */
    @Override
    public void updatePaymentChannelStatus(SysPaymentChannel vo) {
//        SysPaymentChannel entity = new SysPaymentChannel();
//        entity.setMchId(vo.getMchId());
//        entity.setChannelNo(vo.getChannelNo());
//        if (StringUtils.isNotBlank(vo.getService())) {
//            entity.setService(vo.getService());
//        }
//        entity.setMerchantNo(vo.getMerchantNo());
//        entity.setStatus(vo.getStatus());
//        sysPaymentChannelDao.updateByStatus(entity);

        SysPaymentChannel entity = new SysPaymentChannel();
        entity.setId(vo.getId());
        entity.setStatus(vo.getStatus());
        sysPaymentChannelDao.updateByPrimaryKeySelective(entity);
    }


}
